using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Windows.Forms;

using FreeFlow.Administration;

namespace FreeFlowAdministrator
{
	/// <summary>
	/// Summary description for AuthenticationScriptsControl.
	/// </summary>
	public class AuthenticationScriptsControl : AdminUserControl
  {
    private FreeFlowAdministrator.ExtendedDataGrid scriptsGrid;
    private FreeFlowAdministrator.UIActionsControl uiActionsControl;
    private IContainer components;

		public AuthenticationScriptsControl()
		{
			// This call is required by the Windows.Forms Form Designer.
			InitializeComponent();

      UIActionList actionList = new UIActionList();
      actionList.Add("Refresh", "Refresh the list of scripts", new ActionExecuteDelegate(RefreshScripts));
      actionList.Add("Add guest user script", "Add an authentication script for the guest user", 
        new ActionExecuteDelegate(AddGuestScript));
      actionList.Add("Add script", "Add an authentication script", new ActionExecuteDelegate(AddScript));
      actionList.Add("Move up", "Move the selected script up the list", new ActionExecuteDelegate(MoveUp));
      actionList.Add("Move down", "Move the selected script down the list", new ActionExecuteDelegate(MoveDown));
      uiActionsControl.Actions = actionList;
		}

		/// <summary> 
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if(components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Component Designer generated code
		/// <summary> 
		/// Required method for Designer support - do not modify 
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
      this.components = new System.ComponentModel.Container();
      this.scriptsGrid = new FreeFlowAdministrator.ExtendedDataGrid();
      this.uiActionsControl = new FreeFlowAdministrator.UIActionsControl();
      ((System.ComponentModel.ISupportInitialize)(this.scriptsGrid)).BeginInit();
      this.SuspendLayout();
      // 
      // scriptsGrid
      // 
      this.scriptsGrid.AllowNavigation = false;
      this.scriptsGrid.CaptionVisible = false;
      this.scriptsGrid.DataMember = "";
      this.scriptsGrid.Dock = System.Windows.Forms.DockStyle.Fill;
      this.scriptsGrid.HeaderForeColor = System.Drawing.SystemColors.ControlText;
      this.scriptsGrid.Location = new System.Drawing.Point(0, 0);
      this.scriptsGrid.Name = "scriptsGrid";
      this.scriptsGrid.Size = new System.Drawing.Size(578, 472);
      this.scriptsGrid.TabIndex = 0;
      // 
      // uiActionsControl
      // 
      this.uiActionsControl.Actions = null;
      this.uiActionsControl.Dock = System.Windows.Forms.DockStyle.Right;
      this.uiActionsControl.Location = new System.Drawing.Point(578, 0);
      this.uiActionsControl.Name = "uiActionsControl";
      this.uiActionsControl.Size = new System.Drawing.Size(150, 472);
      this.uiActionsControl.TabIndex = 1;
      // 
      // AuthenticationScriptsControl
      // 
      this.Controls.Add(this.scriptsGrid);
      this.Controls.Add(this.uiActionsControl);
      this.Name = "AuthenticationScriptsControl";
      this.Size = new System.Drawing.Size(728, 472);
      ((System.ComponentModel.ISupportInitialize)(this.scriptsGrid)).EndInit();
      this.ResumeLayout(false);

    }
		#endregion

    private AuthenticationScriptCollection authenticationScripts;
    public void LoadScripts(AuthenticationScriptCollection authenticationScripts)
    {
      this.authenticationScripts = authenticationScripts;
      scriptsGrid.DataSource = authenticationScripts;
      scriptsGrid.ClearColumns();
      scriptsGrid.AddColumn("Name");
      scriptsGrid.AddColumn("Script");
    }

    private string MoveUp()
    {
      BindingManagerBase bm = scriptsGrid.BindingContext[scriptsGrid.DataSource];
      int pos = bm.Position;
      if (pos > 0)
      {
        AuthenticationScript script = authenticationScripts[pos];
        authenticationScripts.RemoveAt(pos);
        authenticationScripts.Insert(pos-1, script);
        bm.Position = pos-1;
        scriptsGrid.UnSelect(pos);
        scriptsGrid.Select(pos-1);
        RefreshScripts();
      }
      return null;
    }

    private string MoveDown()
    {
      BindingManagerBase bm = scriptsGrid.BindingContext[scriptsGrid.DataSource];
      int pos = bm.Position;
      if (pos < authenticationScripts.Count-1)
      {
        AuthenticationScript script = authenticationScripts[pos];
        authenticationScripts.RemoveAt(pos);
        authenticationScripts.Insert(pos+1, script);
        bm.Position = pos+1;
        scriptsGrid.UnSelect(pos);
        scriptsGrid.Select(pos+1);
        RefreshScripts();
      }
      return null;
    }

    private string RefreshScripts()
    {
      authenticationScripts.Refresh();
      LoadScripts(authenticationScripts);
      OnRefreshRequired();
      return null;
    }

    private string AddScript()
    {
      using (OpenFileDialog openDialog = new OpenFileDialog())
      {
        openDialog.Filter = "JScript files (*.js)|*.js";
        if (openDialog.ShowDialog() == DialogResult.OK)
        {
          authenticationScripts.Add(openDialog.FileName);
          scriptsGrid.RefreshGrid();
          OnRefreshRequired();
        }
      }
      
      return null;
    }

    private string AddGuestScript()
    {
      using (OpenFileDialog openDialog = new OpenFileDialog())
      {
        openDialog.Filter = "JScript files (*.js)|*.js";
        if (openDialog.ShowDialog() == DialogResult.OK)
        {
          authenticationScripts.AddGuestScript(openDialog.FileName);
          OnRefreshRequired();
        }
      }
      return null;
    }
	}
}
